<?php
/**
 * 设备管理
 *
 * @auther 大耳牛
 * 2017-03-09
 **/
namespace Xijie\Model;
use Think\Model;
class EqmentModel extends Model{
	//定义表名称
	protected $tableName = 'eqment';
	
	/**
	 * 获取总数量
	 *
	 **/
	public function getEqmentCount($where){
		return $this->alias('eqm')->where($where)->count();
	}
	
	/**
	 * 分页获取数据
	 *
	 **/
	public function getEqmentList($page, $where){
		$list = $this->alias('eqm')
					->where($where)
					->order('eqm.roomnumber/1 asc')
					->page($page, C('PAGE_SIZE'))
					->select();
		return $list;
	}
	
	/**
	 * 获取总数量
	 *
	 **/
	public function getEqmentErrorCount($where){
		return M('eqment_error')->alias('eqm')->where($where)->count();
	}
	
	/**
	 * 分页获取数据
	 *
	 **/
	public function getEqmentErrorList($page, $where){
		$list = M('eqment_error')->alias('eqm')
					->where($where)
					->order('eqm.roomnumber/1 asc')
					->page($page, C('PAGE_SIZE'))
					->select();
		return $list;
	}


	/**
	 * 添加设备信息
	 **/
	public function addEqmentInfo($data){
		return $this->data($data)->add();
	}
	
	/**
	 * 根据ID修改设备信息
	 * @param int $id
	 **/
	public function editEqmentInfoByLogId($id, $data){
		return $this->where(array('id' => $id))->setField($data);
	}
	
	public function setEqmentStatusById($id, $status){
		return $this->where(array('id' => $id))->setField('status', $status);
	}
	
	public function getEqmentInfoByEqmnumber($eqmnumber, $typeid){
		$info = $this->where(array('eqmnumber' => $eqmnumber, 'typeid' => $typeid))->find();
		//echo $this->_sql();
		return $info;
	}
	
	public function getEqmentInfoByEqmnumber2($eqmnumber){
		$info = $this->where(array('eqmnumber' => $eqmnumber))->find();
		return $info;
	}
	
	public function getEqmentInfoById($id){
		return $this->where(array('id' => $id))->find();
	}
	
	/**
	 * 根据房间号获取设备列表
	 **/
	public function getEqmentListByRoomnumber($roomnumber){
		return $this->where(array('roomnumber' => $roomnumber))->select();
	}

	/**
	 * 根据设备类型和时间查询抄表日志
	 **/
	public function getEqmentLogListByTypeIdAndTime($page, $where){
		$list = M('eqment_log')
				->where($where)
				->order('id desc')
				->page($page, C('PAGE_SIZE'))
				->select();
		//echo M('eqment_log')->_sql();
		return $list;		
	}

	public function getEqmentLogListCountByTypeIdAndTime($where){
		return M('eqment_log')->where($where)->count();
	}

	/**
	 * 查询抄表日志
	 **/
	public function getEqmentLogListByTypeIdAndWhere($where){
		return M('eqment_log')->where($where)->order('id desc')->select();
	}

	/**
	 * 根据时间查询抄表失败的日志
	 **/
	public function getEqmentLogFailListByTime($typeid, $times){
		return $this->alias('eqment')
					->join("left join momoda_eqment_log as logss on eqment.eqmnumber = logss.eqmnumber and eqment.floor = logss.floor and logss.last_time like '%". $times ."%'")
					->field('eqment.eqmnumber,eqment.roomnumber,eqment.floor,eqment.eqmtitle,eqment.updatetime,eqment.last_number as lnumber,logss.`last_number`')
					->where(array(
						'eqment.typeid' => $typeid
					))->order('eqment.id asc')
					->select();
	}
}